<?php

namespace app\common\logic;

/**
 * 咨询师逻辑
 */
class Consultant extends LogicBase
{

    /**
     * 获取咨询师列表
     */
    public function getConsultantList($where = [], $field = '*', $order = '')
    {
        
        $where[ DATA_STATUS_NAME] = ['neq', DATA_DELETE];
        
        return $this->modelConsultant->getList($where, $field, $order, DB_LIST_ROWS);
    }
    
    
     /**
     * 获取咨询师列表
     */
    public function getIndexConsultantList($where = [], $field = '*', $order = '')
    {
        
        $where[ DATA_STATUS_NAME] = ['neq', DATA_DELETE];
        
        return $this->modelConsultant->getList($where, $field, $order,12,[],'',12);
    }
    
    
    /**
     * 获取咨询师列表搜索条件
     */
    public function getWhere($data = [])
    {
        
        $where = [];
        
        !empty($data['search_data']) && $where['a.name|a.describe'] = ['like', '%'.$data['search_data'].'%'];
        
        return $where;
    }
    
     /**
     * 咨询师信息编辑
     */
    public function consultantEdit($data = [])
    {

        $validate_result = $this->validateConsultant->scene('edit')->check($data);

        if (!$validate_result) {
            
            return [RESULT_ERROR, $this->validateConsultant->getError()];
        }
        
        $url = url('consultantList');   
        
        $handle_text = empty($data['id']) ? '新增' : '编辑';
        
        if($handle_text == '新增'){
            
            $data['created_at'] = time();
        }
        
        $data['updated_at'] = time();

        $result = $this->modelConsultant->setInfo($data);

        $result && action_log($handle_text, '咨询师' . $handle_text . '，name：' . $data['consultant_name']);
        
        return $result ? [RESULT_SUCCESS, '咨询师操作成功', $url] : [RESULT_ERROR, $this->modelConsultant->getError()];
    }
    
    
    /**
     * 首页咨询师信息编辑
     */
    public function indexConsultantEdit($data = [])
    {

        $validate_result = $this->validateConsultant->scene('edit')->check($data);

        if (!$validate_result) {
            
            return [RESULT_ERROR, $this->validateConsultant->getError()];
        }
        
        $url = url('consultant');   
        
        $handle_text = empty($data['id']) ? '新增' : '编辑';
        
        if($handle_text == '新增'){
            
            $data['created_at'] = time();
        }
        
        $data['updated_at'] = time();
        
        $data['is_index'] = 1;

        $result = $this->modelConsultant->setInfo($data);

        $result && action_log($handle_text, '首页咨询师' . $handle_text . '，name：' . $data['consultant_name']);
        
        return $result ? [RESULT_SUCCESS, '首页咨询师操作成功', $url] : [RESULT_ERROR, $this->modelConsultant->getError()];
    }
  
     /**
     * 获取咨询师信息
     */
    public function getConsultantInfo($where = [], $field = 'a.*')
    {
        $this->modelConsultant->alias('a');
       
        $where['a.' . DATA_STATUS_NAME] = ['neq', DATA_DELETE];
        
        return $this->modelConsultant->getInfo($where, $field);
    }
    
    /**
     * 获取咨询师分类列表
     */
    public function getConsultantCategoryList($where = [], $field = true, $order = '', $paginate = 0)
    {
        return $this->modelConsultant->getList($where, $field, $order, $paginate);
    }
}

